Passed
Push — master ( d291c5...c73ec5 )
by
unknown
13:07
created

script.js ➔ menubar   A

Complexity

Conditions 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
/**
2
 * Hide Prosemirror and show the default editor
3
 *
4
 * @param {string} text the wiki syntax to be shown in the textarea
5
 */
6
function showDefaultEditor(text) {
7
    window.Prosemirror.destroyProsemirror();
8
    window.proseMirrorIsActive = false;
9
    dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft);
0 ignored issues
show
Bug introduced by
The variable dw_locktimer seems to be never declared. If this is a global, consider adding a /** global: dw_locktimer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
    jQuery('#wiki__text').val(text).show();
11
    jQuery('#size__ctl').show();
12
    jQuery('.editBox > .toolbar').show();
13
}
14
15
/**
16
 * Hide the default editor and start a new Prosemirror Editor
17
 *
18
 * @param {string} json the prosemirror document json
19
 */
20
function showProsemirror(json) {
21
    const $textArea = jQuery('#wiki__text');
22
    const $prosemirrorJsonInput = jQuery('#dw__editform').find('[name=prosemirror_json]').val(json);
23
    try {
24
        window.Prosemirror.enableProsemirror();
25
        disableNativeFirefoxTableControls();
26
    } catch (e) {
27
        console.error(e);
28
        let message = 'There was an error in the WYSIWYG editor. You will be redirected to the syntax editor in 5 seconds.';
29
        if (window.SentryPlugin) {
30
            SentryPlugin.logSentryException(e, {
0 ignored issues
show
Bug introduced by
The variable SentryPlugin seems to be never declared. If this is a global, consider adding a /** global: SentryPlugin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
31
                tags: {
32
                    plugin: 'prosemirror',
33
                    'id': JSINFO.id,
0 ignored issues
show
Bug introduced by
The variable JSINFO seems to be never declared. If this is a global, consider adding a /** global: JSINFO */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
34
                },
35
                extra: {
36
                    'content': $textArea.val(),
37
                    'json': $prosemirrorJsonInput.val(),
38
                }
39
            });
40
            message += ' -- The error has been logged to Sentry.';
41
        }
42
        showErrorMessage(message);
43
        setTimeout(function() {
44
            jQuery('.plugin_prosemirror_useWYSIWYG').click();
45
        }, 5000);
46
    }
47
    window.proseMirrorIsActive = true;
48
    $textArea.hide();
49
    jQuery('#size__ctl').hide();
50
    jQuery('.editBox > .toolbar').hide();
51
    jQuery('div.ProseMirror').focus();
52
53
    if (dw_locktimer.addField) {
0 ignored issues
show
Bug introduced by
The variable dw_locktimer seems to be never declared. If this is a global, consider adding a /** global: dw_locktimer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
54
        // todo remove this guard after the next stable DokuWiki release after Greebo
55
        dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft, 'prosemirror__editor');
56
        dw_locktimer.addField('input[name=prosemirror_json]');
57
    } else {
58
        console.warn('Draft saving in WYSIWYG is not available. Please upgrade your wiki to the current development snapshot.')
59
    }
60
}
61
62
/**
63
 * Disables Firefox's controls for editable tables, they are incompatible with prosemirror
64
 *
65
 * See https://github.com/ProseMirror/prosemirror/issues/432 and https://github.com/ProseMirror/prosemirror-tables/issues/22
66
 */
67
function disableNativeFirefoxTableControls() {
68
    document.execCommand("enableObjectResizing", false, "false");
69
    document.execCommand("enableInlineTableEditing", false, "false");
70
}
71
72
/**
73
 * Initialize the prosemirror framework
74
 *
75
 * (This shouldn't do much until we actually use the editor, but we maybe shouldn't do this twice)
76
 */
77
function initializeProsemirror() {
78
    try {
0 ignored issues
show
Unused Code introduced by
Empty try statement found. This statement can be removed.
Loading history...
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
79
        /* DOKUWIKI:include lib/bundle.js */
80
    } catch (e) {
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
81
        const $textArea = jQuery('#wiki__text');
0 ignored issues
show
Bug introduced by
The variable $textArea seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$textArea.
Loading history...
82
        console.error(e);
83
        let message = 'There was an error initializing the WYSIWYG editor.';
0 ignored issues
show
Bug introduced by
The variable message seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.message.
Loading history...
84
        if (window.SentryPlugin) {
85
            SentryPlugin.logSentryException(e, {
86
                tags: {
87
                    plugin: 'prosemirror',
88
                    'id': JSINFO.id,
89
                },
90
                extra: {
91
                    'content': $textArea.val(),
92
                }
93
            });
94
            message += ' The error has been logged to sentry.';
95
        }
96
97
        showErrorMessage(message);
98
99
        DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', '');
100
    }
101
}
102
103
/**
104
 * Add the error message above the editor
105
 *
106
 * @param {string} errorMsg
107
 */
108
function showErrorMessage(errorMsg) {
109
    jQuery('.editBox').before(
110
        jQuery('<div class="error"></div>').text(errorMsg)
111
    );
112
}
113
114
/**
115
 * Switch between WYSIWYG and Syntax editor
116
 */
117
function toggleEditor() {
118
    const $textArea = jQuery('#wiki__text');
119
    const $jsonField = jQuery('#dw__editform').find('[name=prosemirror_json]');
120
    jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', {
0 ignored issues
show
Bug introduced by
The variable DOKU_BASE seems to be never declared. If this is a global, consider adding a /** global: DOKU_BASE */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
121
        call: 'plugin_prosemirror_switch_editors',
122
        data: window.proseMirrorIsActive ? $jsonField.val() : $textArea.val(),
123
        getJSON: window.proseMirrorIsActive ? '0' : '1',
124
    }).done(function handleSwitchEditorResponse(data) {
125
        if (window.proseMirrorIsActive) {
126
            showDefaultEditor(data.text);
127
        } else {
128
            showProsemirror(data.json);
129
        }
130
    }).fail(function (jqXHR, textStatus, errorThrown) {
131
        console.error(jqXHR, textStatus, errorThrown); // FIXME: proper error handling
132
        if (jqXHR.responseJSON && jqXHR.responseJSON.error) {
133
            showErrorMessage(jqXHR.responseJSON.error);
134
        } else {
135
            let message = 'The request failed with an unexpected error.';
136
            if (window.SentryPlugin) {
137
                SentryPlugin.logSentryException(new Error(textStatus), {
0 ignored issues
show
Bug introduced by
The variable SentryPlugin seems to be never declared. If this is a global, consider adding a /** global: SentryPlugin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
138
                    tags: {
139
                        plugin: 'prosemirror',
140
                        'id': JSINFO.id,
0 ignored issues
show
Bug introduced by
The variable JSINFO seems to be never declared. If this is a global, consider adding a /** global: JSINFO */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
141
                        status: jqXHR.status
142
                    },
143
                    extra: {
144
                        'content': $textArea.val(),
145
                        'responseText': jqXHR.responseText,
146
                    }
147
                });
148
                message += ' -- The error has been logged to Sentry.';
149
            }
150
            showErrorMessage(message);
151
        }
152
    });
153
154
    const $current = DokuCookie.getValue('plugin_prosemirror_useWYSIWYG');
0 ignored issues
show
Bug introduced by
The variable DokuCookie seems to be never declared. If this is a global, consider adding a /** global: DokuCookie */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
155
    DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', $current ? '' : '1');
156
}
157
158
/**
159
 * If the cookie is set, then show the WYSIWYG editor and add the switch-editor-event to the button
160
 */
161
function handleEditSession() {
162
    const $jsonField = jQuery('#dw__editform').find('[name=prosemirror_json]');
163
    if (DokuCookie.getValue('plugin_prosemirror_useWYSIWYG')) {
0 ignored issues
show
Bug introduced by
The variable DokuCookie seems to be never declared. If this is a global, consider adding a /** global: DokuCookie */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
164
        showProsemirror($jsonField.val());
165
    }
166
    const $toggleEditorButton = jQuery('.plugin_prosemirror_useWYSIWYG');
167
    $toggleEditorButton.on('click', toggleEditor);
168
}
169
170
/**
171
 * when the editor switch button moves out of the view-port, the menubar gets a class
172
 * @see https://codepen.io/hey-nick/pen/mLpmMV
173
 * @see https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
174
 */
175
176
function menubar() {
177
    const editorswitch = document.querySelector('button[name=prosemirror]');
178
    const menubar = document.querySelector('#prosemirror__editor div.menubar');
179
    const observer = new IntersectionObserver(
0 ignored issues
show
Bug introduced by
The variable IntersectionObserver seems to be never declared. If this is a global, consider adding a /** global: IntersectionObserver */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
180
        ([e]) => {
181
            return menubar.classList.toggle('prosemirror-menubar-fixed', e.intersectionRatio !== 1);
182
        },
183
        {
184
            root: null,
185
            threshold: [0, 1]
186
        }
187
    );
188
    observer.observe(editorswitch);
189
}
190
191
192
jQuery(function () {
193
    initializeProsemirror();
194
    window.proseMirrorIsActive = false;
195
196
    jQuery(menubar);
197
198
    if (jQuery('#dw__editform').find('[name=prosemirror_json]').length) {
199
        handleEditSession();
200
    }
201
202
    jQuery(window).on('fastwiki:afterSwitch', function(evt, viewMode, isSectionEdit, prevViewMode) {
0 ignored issues
show
Unused Code introduced by
The parameter prevViewMode is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
203
        if (viewMode === 'edit' || isSectionEdit) {
204
            handleEditSession();
205
        }
206
    });
207
});
208